package map.coreui.fragment

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.zjw.zy.R
import com.zjw.zy.ext.*
import com.zjw.zy.utils.PhoneUtils
import map.constant.MapMode
import map.coreui.view.PoiDetailView
import map.viewmodel.*

class MapFragment : MapBaseFragment() {

    private val mapViewModel: MapViewModel by viewModels()
    private val searchViewModel: SearchViewModel by viewModels()
    private val previewViewModel: PreviewViewModel by viewModels()
    private val routeViewModel: RoutePlanViewModel by activityViewModels()
    private val mapFunctionViewModel: MapFunctionViewModel by activityViewModels()

    private val mapModeButton: ImageView by lazy { findViewById(R.id.map_mode_button) }
    private val poiSearch: ImageView by lazy { findViewById(R.id.poi_search) }
    private val poiSearchInput: EditText by lazy { findViewById(R.id.poi_search_input) }
    private val poiDetail: PoiDetailView by lazy { findViewById(R.id.poi_detail) }
    private val suggestSearch: ImageView by lazy { findViewById(R.id.suggest_search_button) }

    override fun onPageEnter() {
        //跟随态
        mapViewModel.onPageEnter()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        mapViewModel.getMapMode().observe(this) {
            when (it) {
                MapMode.NORMAL_MAP -> {
                }

                MapMode.SATELLITE_MAP -> {
                }
            }
        }

        mapViewModel.getPoiDetails().observe(this) {
            onPoiDetails(it)
        }

        mapViewModel.getMapTouch().observe(this) {
            poiSearchInput.setText("")
            poiSearchInput.visibility = View.GONE
        }
        mapFunctionViewModel.getLoveLatLng().observe(this) {
            go(R.id.nav_map_lover_location_fragment)
        }
    }

    private fun onPoiDetails(poiDetailResult: PoiDetailResult?) {
        if (poiDetailResult == null) {
            poiDetail.visibility = View.GONE
            return
        }
        //展示此poi
        poiDetail.visibility = View.VISIBLE
        poiDetail.bindData(poiDetailResult)
    }

    override fun onBindViewListener() {
        mapModeButton.setOnClickListener {
            mapViewModel.switchMapMode()
        }

        poiSearch.setOnClickListener {
            if (poiSearchInput.visibility == View.GONE) {
                poiSearchInput.visibility = View.VISIBLE
            } else {
                //do search
                val keyWord = poiSearchInput.value()
                if (keyWord.isEmpty() || keyWord.trim().isEmpty()) {
                    poiSearchInput.visibility = View.GONE
                    return@setOnClickListener
                }
                mapViewModel.toSelfLocation(false)
                searchViewModel.nearSearch(keyWord)
            }
        }

        poiDetail.bindListener(object : PoiDetailView.PoiDetailListener {
            override fun onClickGo(poi: PoiDetailResult) {
                routeViewModel.walkPlan(poi.location) {
                    if (it) {
                        //规划成功，跳转规划页面
                        go(R.id.nav_map_multiroute_fragment)
                    } else {
                        "路线规划失败".longToast()
                    }
                }
            }

            override fun onClickDetails(detailUrl: String) {
                //详情网页
            }

            override fun onClickPhone(text: CharSequence?) {
                alertConfirm(title = "联系商家？", activity = requireActivity()) {
                    if (it) {
                        PhoneUtils.call(it.toString())
                    }
                }
            }
        })

        suggestSearch.setOnClickListener {
            mapViewModel.toSelfLocation(false)
            go(R.id.nav_map_suggest_search_fragment)
        }
    }

    override fun onBindViewValue() {

    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fu_fragment
    }

    override fun onPageExit() {
        mapViewModel.onPageExit()
    }

    override fun onBackClick() {
        mapActivity().finish()
    }
}